$(function(){
	//点击选择地址
	$(".address_info").on("click",function(){
		
		$(".select_address").before($("<div class='shadow'></div>"))
		$(".select_address").css({
			opacity:"1",
			visibility:"visible"
		});
		clickShadow(function(){
			$(".select_address").css({
				opacity:"0",
				visibility:"hidden"
			})
		})
	});
	
//点击增加地址
$(".select_header").on("click",".floatRight",function(){
	$(".select_address").css({
			opacity:"0",
			visibility:"hidden"
	})
	$(".add_address").css({
			opacity:"1",
			visibility:"visible"
	})
	clickShadow(function(){
		$(".add_address").css({
			opacity:"0",
			visibility:"hidden"
		})
	})
})
//点击返回
$(".address_return").on("click",function(){
	$(".add_address").css({
			opacity:"0",
			visibility:"hidden"
	})
	$(".select_address").css({
			opacity:"1",
			visibility:"visible"
	})
});
//省级地址下拉框发生改变
$("#province").change(function(){
	var provinceId = $(this).val();
	var provinceName = $(this).find("option[value="+provinceId+"]").text();
	if(provinceName=='省份（必填）'){
		$("#city").empty();
		$("#city").prepend("<option value='city'>市（选填）</option>");
		$("#county").empty();
		$("#county").prepend("<option value='county'>县（选填）</option>");
	}else{
		$.ajax({
			type:"get",
			url:base_url+"customer/getAddressByParentId",
			data:{"parentId":provinceId},
			async:false,
			success:function(data){
				$("#city").empty();
				$("#city").prepend("<option value='city'>市（选填）</option>");
				$("#county").empty();
				$("#county").prepend("<option value='county'>县（选填）</option>");
				for(var i=0;i<data.length;i++){
					$("#city").append('<option value='+data[i].addressId+'>'+data[i].addressName+'</option>');
				}
			},
			error:function(){
			}
		});
	}
});
//市级下拉框发生改变
$("#city").change(function(){
	var cityId = $(this).val();
	var cityName = $(this).find("option[value="+cityId+"]").text();
	if(cityName=='市（选填）'){
		$("#county").empty();
		$("#county").prepend("<option value='county'>县（选填）</option>");
	}else{
		$.ajax({
			type:"get",
			url:base_url+"customer/getAddressByParentId",
			data:{"parentId":cityId},
			async:false,
			success:function(data){
				$("#county").empty();
				$("#county").prepend("<option value='county'>县（选填）</option>");
				for(var i=0;i<data.length;i++){
					$("#county").append('<option value='+data[i].addressId+'>'+data[i].addressName+'</option>');
				}
			},
			error:function(){
			}
		});
	}
});
//增加新的收货地址
$("#saveNewAddress").on("click",function(){
	var name = $("#name").val();
	var phone = $("#phone").val();
	var detailAddress = $("#detailAddress").val();
	var provinceId = $("#province").val();
	var provinceName = $("#province").find("option[value="+provinceId+"]").text();
	var isDefault;
	if(name!=""&&phone!=""&&detailAddress!=""&&provinceId!="province"){
		var cityId = $("#city").val();
		var cityName =$("#city").find("option[value="+cityId+"]").text();
		var newCityName;
		if(cityId=="city"){
			cityId = 0;
			cityName = null
			newCityName = "";
		}else{
			newCityName = cityName;
		}
		var countyId = $("#county").val();
		var countyName =$("#county").find("option[value="+countyId+"]").text();
		var newCountyName;
		if(countyId=="county"){
			countyId = 0;
			countyName = null;
			newCountyName = "";
		}else{
			newCountyName = countyName;
		}
		if($(".select").hasClass("iconfont")){
			isDefault = "1";
		}else{
			isDefault = "0";
		}
		var json = {
				"oneAddressId" : provinceId,
				"oneAddressName" : provinceName,
				"twoAddressId" : cityId,
				"twoAddressName" : cityName,
				"threeAddressId" : countyId,
				"threeAddressName" : countyName,
				"detailAddress" : detailAddress,
				"contactsPerson" : name,
				"contactsWay" : phone,
				"isDefaultReceiptAddress" : isDefault
		}
		var jsonStr = JSON.stringify(json);
		$.ajax({
			type:"get",
			url:base_url+"customer/addBuyerReceiptAddress",
			data:{"jsonStr":jsonStr},
			async:false,
			success:function(data){
				var result = data.retMsg;
				if(result=='true'){
					if(isDefault == "1"){
						$(".select_address").find(".block").each(function(){
							$(this).find(".iconfont").remove();
						});
						$(".select_address").append('<div class="block" name="userReceiptAddress">'
								+'<input type="hidden" id="userReceiptAddressMsg"'
								+' data-receiptAddressId = ' + data.data
								+' data-oneAddressId = '+ provinceId
								+' data-oneAddressName = '+ provinceName
								+' data-twoAddressId = ' + cityId
								+' data-twoAddressName = ' + newCityName
								+' data-threeAddressId = ' + countyId 
								+' data-threeAddressName = ' + newCountyName
								+' data-contactsWay = ' + phone
								+' data-contactsPerson = ' + name
								+' data-detailAddress = '+ detailAddress +'>'
				+'<p>姓名：<span name="userReceiptAddressName">'+name+'</span></p>'
				+'<p>电话：<span name="userReceiptAddressPhone">'+phone+'</span></p>'
				+'<p>地址：<span name="userReceiptAddressDetail">'+provinceName+newCityName+newCountyName+detailAddress+'</span></p>'
				+'<i class="iconfont default">&#xe644;</i>'
				+'</div>');
					}else{
						if($("#chooseReceiptAddressName")[0]==undefined){
							$(".select_address").append('<div class="block" name="userReceiptAddress">'
									+'<input type="hidden" id="userReceiptAddressMsg"'
									+' data-receiptAddressId = '+data.data
									+' data-oneAddressId = '+ provinceId
									+' data-oneAddressName = '+ provinceName
									+' data-twoAddressId = ' + cityId
									+' data-twoAddressName = ' + newCityName
									+' data-threeAddressId = ' + countyId 
									+' data-threeAddressName = ' + newCountyName
									+' data-contactsWay = ' + phone
									+' data-contactsPerson = ' + name
									+' data-detailAddress = '+ detailAddress+'>'
									+'<p>姓名：<span name="userReceiptAddressName">'+name+'</span></p>'
									+'<p>电话：<span name="userReceiptAddressPhone">'+phone+'</span></p>'
									+'<i class="iconfont default">&#xe644;</i>'
									+'<p>地址：<span name="userReceiptAddressDetail">'+provinceName+newCityName+newCountyName+detailAddress+'</span></p></div>');
						}else{
							$(".select_address").append('<div class="block" name="userReceiptAddress">'
									+'<input type="hidden" id="userReceiptAddressMsg"'
									+' data-receiptAddressId = '+ data.data
									+' data-oneAddressId = '+ provinceId
									+' data-oneAddressName = '+ provinceName
									+' data-twoAddressId = ' + cityId
									+' data-twoAddressName = ' + newCityName
									+' data-threeAddressId = ' + countyId 
									+' data-threeAddressName = ' + newCountyName
									+' data-contactsWay = ' + phone
									+' data-contactsPerson = ' + name
									+' data-detailAddress = '+ detailAddress+'>'
									+'<p>姓名：<span name="userReceiptAddressName">'+name+'</span></p>'
									+'<p>电话：<span name="userReceiptAddressPhone">'+phone+'</span></p>'
									+'<p>地址：<span name="userReceiptAddressDetail">'+provinceName+newCityName+newCountyName+detailAddress+'</span></p></div>');
						}
						
					}
					$(".add_address").css({
						opacity:"0",
						visibility:"hidden"
					})
					$(".select_address").css({
							opacity:"1",
							visibility:"visible"
					})
					tip.content("添加收货地址成功").show();
				}else{
					tip.content("添加收货地址失败").show();
				}
			},
			error:function(){
			}
		});
		
	}else{
		tip.content("必填项不能为空!").show();
	}
});
//设为默认地址按钮
$("#beDefault").on("click",function(){
	$(this).toggleClass("iconfont").toggleClass("icon-icon");
});
//用户选择收货地址
$(".select_address").on("click",'div[name="userReceiptAddress"]',function(){

	var receiptAddressId = $(this).find("#userReceiptAddressMsg").attr("data-receiptAddressId");
	var productId = $("#productMsg").attr("data-productId");
	var productItemId = $("#productMsg").attr("data-productItemId");
	var amount = $("#productMsg").attr("data-amount");
	var json = {
			"receiptAddressId":receiptAddressId,
			"productId":productId,
			"productItemId":productItemId,
			"amount":amount
			
	}
	var jsonStr = JSON.stringify(json);
	window.location.href = base_url + "customer/changeBuyDiscountProduct?jsonStr="+jsonStr;

});
//提交订单时进行数据的检验
$(".item_buy").on("click",function(){
	var chooseReceiptAddress = $("#chooseReceiptAddressMsg");
	var allCount = $("#allCount").text();
	var allProductCount = $("#allProductCount").text();
	var allFreight = $("#allFreight").text();
	if($("#chooseReceiptAddressName")[0]==undefined){
		tip.content("请选择一个收货地址").show();
	}else{
		var productId = $("#productMsg").attr("data-productId");
		var productItemId = $("#productMsg").attr("data-productItemId");
		var amount = $("#productMsg").attr("data-amount");
		$.ajax({
			type:"get",
			url:base_url+"customer/checkoutDiscountProductMsgBeforeBuy",
			data:{"productItemId":productItemId,"productId":productId,"amount":amount},
			async:false,
			success:function(data){
				if(data.retMsg==null){
					var json = {
							"productId":productId,
							"productItemId":productItemId,
							"amount":amount,
							"allCount": allCount,
							"allProductCount" : allProductCount,
							"allFreight" : allFreight,
							"oneAddressId": chooseReceiptAddress.attr("data-oneAddressId"),
							"oneAddressName":chooseReceiptAddress.attr("data-oneAddressName"),
							"twoAddressId":chooseReceiptAddress.attr("data-twoAddressId"),
							"twoAddressName":chooseReceiptAddress.attr("data-twoAddressName"),
							"threeAddressId":chooseReceiptAddress.attr("data-threeAddressId"),
							"threeAddressName":chooseReceiptAddress.attr("data-threeAddressName"),
							"contactsWay":chooseReceiptAddress.attr("data-contactsWay"),
							"contactsPerson":chooseReceiptAddress.attr("data-contactsPerson"),
							"detailAddress":chooseReceiptAddress.attr("data-detailAddress"),
					}
					var jsonStr = JSON.stringify(json);
					post(base_url+"pay/payOrdersBuyDiscountProduct",{"jsonStr":jsonStr});
				}else{
					$(".item_container").prepend('<p class="ribbon">'+data.retMsg+'</p>');
				}
			},
			error:function(){
			}
		});
	}
	
});

//查看卖家的运费系统
$(".postage").on("tap",function(){
	
	$(".check-postage").before("<div class='shadow'></div>");
	$(".check-postage").find("tbody").empty();
	var sellerId = $(this).find("button").attr("data-sellerId"); 
	$.ajax({
		type:"get",
		url:base_url+"customer/getShopPostageMsgs",
		data:{"sellerId":sellerId},
		async:false,
		success:function(data){
			console.log(data);
			var othersArea = '<tr>'
				+'<td>中国大陆</td>'
				+'<td>'+data.deProductFreight+'元</td>'
				+'<td>'+data.deFulfilWithoutFreight+'元</td>'
				+'</tr>';
			$(".check-postage").find("tbody").append(othersArea);
			var postageMsgs = data.postageMsgs;
			for(var i=0;i<postageMsgs.length;i++){
				var area = '<tr>'
					+'<td>'+postageMsgs[i].provinceName+'</td>'
					+'<td>'+postageMsgs[i].postageSystem.productFreight+'元</td>'
					+'<td>'+postageMsgs[i].postageSystem.fulfilWithoutFreight+'元</td>'
					+'</tr>'
					$(".check-postage").find("tbody").append(area);
			}
		},
		error:function(){
		}
	});
	$(".check-postage").fadeIn();
	clickShadow(function(){
		$(".check-postage").fadeOut();
	})
});

});



window.onscroll = function(){
	lazyLoad($(".lazy[data-load='false']"));
}